<?php
namespace Sys\Controller;

use Think\Image;
use Todo\Controller\ApiController;
use Todo\Service\FileService;
use Todo\Service\LoginService;

class UserController extends ApiController
{
    /**
     * 自定义查询条件
     * @param array &$where 丢生成的查询条件
     */
    protected function _filter(&$where)
    {
        $keyword = I('request.keywords/s');
        if ($keyword) $where['_string'] = "account like '%" . $keyword . "%' OR nickname like '%" . $keyword . "%' OR email like '%" . $keyword . "%' OR mobile like '%" . $keyword . "%'";
    }

    /**
     * 个人资料，get获取、post更改
     */
    public function my()
    {
        $this->_loadModel();
        if (IS_GET) {
            $info = $this->_model->field('nickname,mobile,email,photo')->keyFind($this->_user['id']);
            $this->apiData($info);
        } else {
            $data = array(
                'nickname' => I('post.nickname/s'),
                'mobile' => I('post.mobile/s'),
                'email' => I('post.email/s')
            );
            if (!$data['nickname'] || !$data['email'] || !$data['mobile']) $this->apiError('请检查您填写的信息!');
            if (!$data) $this->apiError($this->_model->getError());

            $data['id'] = $this->_user['id'];
            if ($this->_model->save($data) === false) {
                $this->apiError($this->_model->getError());
            } else {
                $this->apiSuccess('更新个人信息成功!');
            }
        }
    }

    /**
     * 修改密码
     */
    public function password()
    {
        $this->_loadModel();
        if (IS_POST) {
            $oldpass = I('post.oldpass/s');
            if (!$oldpass || strlen($oldpass) != 32) $this->apiError('原始密码有误!');
            $newpass = I('post.newpass/s');
            if (!$newpass || strlen($newpass) != 32) $this->apiError('新密码不合要求!');

            $userInfo = $this->_model->keyFind($this->_user['id']);
            if ($userInfo['password'] != LoginService::getInstance()->encrypt($oldpass, $userInfo['salt'])) $this->apiError('原始密码有误!');

            $data = array();
            $data['password'] = LoginService::getInstance()->encrypt($newpass, $userInfo['salt']);
            $data['id'] = $this->_user['id'];
            if ($this->_model->save($data) === false) {
                $this->apiError($this->_model->getError());
            } else {
                $this->apiSuccess('更新密码成功!');
            }
        } else {
            $this->apiError('不支持的操作!');
        }
    }

    /**
     * 更新用户头像
     */
    public function photo()
    {
        $imagePath = I('post.img');
        if (!$imagePath || !FileService::getInstance()->checkFileExist($imagePath)) $this->apiError("请先上传头像文件!");

        $x = I('post.x/d', 0);
        $y = I('post.y/d', 0);
        $w = I('post.w/d', 200);
        $h = I('post.h/d', 200);

        $faceImage = 'faces/' . date('Y');
        FileService::getInstance()->mkDir($faceImage);
        $faceImage .= '/' . $this->_user['id'] . '.jpg';

        $image = new Image();
        $image->open(C('FILE_UPLOAD.rootPath') . $imagePath);
        $image->thumb(400, 1000);
        $image->crop($w, $h, $x, $y);
        $image->save(C('FILE_UPLOAD.rootPath') . $faceImage, 'jpg');
        if (!FileService::getInstance()->checkFileExist($faceImage)) $this->apiError("保存头像文件失败!");

        $this->_user['photo'] = $faceImage;
        session('user', $this->_user);

        $this->_loadModel();
        if ($this->_model->save($this->_user) === false) $this->apiError("保存头像失败!");


        $this->apiData($faceImage, "保存头像成功!");
    }
}